import React from "react";
import {connect} from "react-redux";
import {withRouter} from "react-router-dom"
import axios from "axios";
import {loadData} from "../../redux/user.reducer";


const pathList = ["/register", "/login"];

@withRouter
@connect(
  null,
  {loadData}
)
class Auth extends React.Component {

  componentDidMount () {
    axios.get("/user/info").then(res => {
      if (res.status === 200 && res.data.code === 0) {
        //已经登录
        this.props.loadData(res.data.data);
      } else {
        // 未登录且不再注册和登录页面
        const path = this.props.history.location.pathname;
        if (pathList.indexOf(path) === -1) {
          this.props.history.push("/login");
        }
      }
    });
  }

  render () {
    return null;
  }
}

export default Auth;